package com.xsoft.framework.web;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.CookiesAware;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;



import com.xsoft.framework.po.Result;
import com.xsoft.framework.util.Page;
import com.opensymphony.xwork2.ActionSupport;

public abstract class BasicAction extends ActionSupport implements
		SessionAware, ServletRequestAware, ServletResponseAware, CookiesAware {
	
	protected HttpServletRequest request;
	protected HttpServletResponse response;
	protected Map session;
	protected Map cookies;
	protected int pageSize = 20;
	protected int firstPage = 1;
	protected int lastPage;
	protected int nextPage;
	protected int prePage;
	protected int gotoPage = 1;
	protected int countPage;
	protected int countData;
	protected Date today = new Date();
	protected String inputUrl;
	protected Log log = LogFactory.getLog(getClass());
	private String message;// 页面提示信息
	private String localationURL;// 页面重定位地址
    private String showXML;//输出XML内容
	
    protected Result result; // 返回页面或ajax返回对象的数据
    
    
    
    
    /**
     * 存放一般的数据。data可以为一个集合list
     * @param code
     * @param info
     * @param data
     */
    protected void createCommonResult(int code,String info,Object data) {
		result = new Result();
		result.setCode(code);
		result.setInfo(info);
		result.setData(data);
	}
    
    
    protected void createErrorResult(String info) {
		result = new Result();
		result.setCode(-1);
		result.setInfo(info);
	}
    
    /**
     * 存放分页的数据
     * @param code
     * @param info
     * @param page
     */
    protected void createPaginationResult(int code, String info,Page page) {
		result = new Result();
		result.setCode(code);
		result.setInfo(info);
		result.setData(page);
	}
	
	
	
	public Result getResult() {
		return result;
	}

	public void setResult(Result result) {
		this.result = result;
	}

	public String getLocalationURL() {
		return localationURL;
	}

	public void setLocalationURL(String localationURL) {
		this.localationURL = localationURL;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getInputUrl() {
		return inputUrl;
	}

	public void setInputUrl(String inputUrl) {
		this.inputUrl = inputUrl;
	}

	public Date getToday() {
		return today;
	}

	public int getCountPage() {
		return countPage;
	}

	public void setCountPage(int countPage) {
		this.countPage = countPage;
	}

	public int getCountData() {
		return countData;
	}

	public void setCountData(int countData) {
		this.countData = countData;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getFirstPage() {
		return firstPage;
	}

	public void setFirstPage(int firstPage) {
		this.firstPage = firstPage;
	}

	public int getLastPage() {
		return lastPage;
	}

	public void setLastPage(int lastPage) {
		this.lastPage = lastPage;
	}

	public int getNextPage() {
		return nextPage;
	}

	public void setNextPage(int nextPage) {
		this.nextPage = nextPage;
	}

	public int getPrePage() {
		return prePage;
	}

	public void setPrePage(int prePage) {
		this.prePage = prePage;
	}

	public int getGotoPage() {
		return gotoPage;
	}

	// public void setGotoPage(Object gotoPage) {
	// this.gotoPage = gotoPage;
	// }

	public void setSession(Map session) {
		this.session = session;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
		try {
			gotoPage = Integer.parseInt(request.getParameter("gotoPage"));
		} catch (NumberFormatException e) {
			gotoPage = 1;
		}
		this.inputUrl = request.getRequestURI();
	}

	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	public void setCookiesMap(Map cookies) {
		this.cookies = cookies;
	}

	public void setPageInfo(Page page) {
		this.countData = new Long(page.getTotalCount()).intValue();
		this.countPage = new Long(page.getTotalPageCount()).intValue();
		this.nextPage = this.gotoPage + 1;
		if (this.nextPage > this.countPage) {
			this.nextPage = this.countPage;
		}
		this.prePage = this.gotoPage - 1;
		if (this.prePage < 1) {
			this.prePage = 1;
		}
		this.lastPage = this.countPage;
	}

	public String getBasePath() {
		String basePath = request.getScheme() + "://" + request.getServerName()
				+ ":" + request.getServerPort() + getContextPath() + "/";
		return basePath;
	}

	public String getServletPath() {
		String servlet_path = request.getServletPath();
		return servlet_path;
	}

	public String getContextPath() {
		String context_path = request.getContextPath();
		return context_path;
	}

	public String getApplicationRealPath() {
		return this.request.getRealPath("/");
	}

	public String getApplicationRealPath(String fold) {
		return this.request.getRealPath(fold);
	}


	protected ApplicationContext getApplicationContext() {
		return WebApplicationContextUtils.getWebApplicationContext(ServletActionContext
				.getServletContext());
	}

	public String getShowXML() {
		return showXML;
	}

	public void setShowXML(String showXML) {
		this.showXML = showXML;
	}
	
	public String getFilterParamete(String value){
		try {
			return URLDecoder.decode(value, "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
}
